﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.DO;
using SES.CMS.DAL;
using SES.CMS.BL;
using SES.CMS.AdminCP;

namespace SES.CMS.AdminCP
{
    public partial class ListOrigin : System.Web.UI.Page
    {
        cmsOriginDO objOrigin = new cmsOriginDO();
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                View();
            }
        }

        void View()
        {
            gvAt.DataSource = new cmsOriginBL().SelectAll();
            gvAt.DataBind();
            pnInsert.Visible = false;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnInsert.Visible = true;
            Insert = true;
        }
        private void initObject()
        {
            objOrigin = new cmsOriginBL().Select(objOrigin);
            objOrigin.Origin = txtName.Text;
            objOrigin.Description = txtDescription.Text;
           
        }
        protected void gvAt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAt.PageIndex = e.NewPageIndex;
        }

        protected void gvAt_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvAt_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new cmsOriginBL().Delete(new cmsOriginDO { OriginID=Convert.ToInt32(gvAt.DataKeys[e.RowIndex].Value)});
            Functions.Alert("Xóa bản ghi thành công", Request.Url.ToString());
        }

        protected void gvAt_SelectedIndexChanged1(object sender, EventArgs e)
        {
            int OriginID = int.Parse(gvAt.DataKeys[gvAt.SelectedIndex].Value.ToString());
            objOrigin.OriginID = OriginID;
            objOrigin = new cmsOriginBL().Select(objOrigin);
            txtName.Text = objOrigin.Origin;
            txtDescription.Text = objOrigin.Description;
            pnInsert.Visible = true;
            pnView.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            

            if (Insert)
            {

                new cmsOriginBL().Insert(objOrigin);
            }
            else
            {
                int OriginID = int.Parse(gvAt.DataKeys[gvAt.SelectedIndex].Value.ToString());
                objOrigin.OriginID = OriginID;
                new cmsOriginBL().Update(objOrigin);
            }
            Functions.Alert("Cập nhật thành công!");
            View();
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            View();
        }
    }
}